<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #coverUrlSlot="{ model, field }">
        <Spin :spinning="spinning">
          <Upload
            v-model:file-list="fileList"
            name="file"
            list-type="picture-card"
            :show-upload-list="false"
            :action="uploadImgApi"
            :before-upload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="model[field]" :src="model[field]" alt="avatar" />
            <div v-else>
              <div class="ant-upload-text">点击上传</div>
            </div>
          </Upload>
        </Spin>
        <div class="hint">建议图片尺寸：宽750*高440rpx</div>
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { accessoriesFormSchema } from './accessories.data';
  import { Spin, Upload } from 'ant-design-vue';
  import { AddApi, EditApi } from '/@/api/set/account';

  export default defineComponent({
    name: 'AccountModal',
    components: { BasicModal, BasicForm, Spin, Upload },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const spinning = ref(false);
      const fileList = ref<any[]>([]);
      const uploadImgApi = () => {};

      const [registerModal, { setModalProps, closeModal }] = useModalInner((data) => {
        resetFields();
        isUpdate.value = !!data?.isUpdate;
        const record = data.record;
        if (unref(isUpdate)) {
          setFieldsValue({
            ...record,
          });
        }
      });
      const [registerForm, { setFieldsValue, resetFields, validate, validateFields }] = useForm({
        labelWidth: 100,
        schemas: accessoriesFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });
      const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });
          const values = await validate();
          if (values.id > 0) {
            await EditApi(values);
          } else {
            await AddApi(values);
          }
          closeModal();
          resetFields();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      // 上传前
      const beforeUpload = () => {
        console.log('beforeUpload');
      };

      const handleChange = () => {
        console.log('handleChange');
      };

      return {
        registerModal,
        registerForm,
        getTitle,
        handleSubmit,
        spinning,
        fileList,
        uploadImgApi,
        beforeUpload,
        handleChange,
      };
    },
  });
</script>
